;;(load "err-ut.scm")
;;(load "cons.scm")
;;(load "sym.scm")
;;(load "err.scm")
;;(load "deli.scm")
;;(load "aux.scm")
;;(load "travel.scm")
;;(load "merge.scm")
;;(load "yexp-aux.scm")
;;(load "yexp-rew.scm")
;;(load "yexp-sexp.scm")
;;(load "sexp.scm")
;;(load "at.scm")
;;(load "and.scm")
(def/err (yexp s n ls rs)
  (let ((len (string-length s))
	(str-l '())
	(cyl-l '())
	(fn-l '())
	(par-l '())
	(st '())
	(fn-str "")
	(par-num 0)
	(pure-yexp/1class-flag #t)
	(rest-define/flag #f)
	(rest/flag #f)
	(rest-call/flag #f)
	(res '())
	(res-str "")
	(res-fn-str "")
	(res-par-num 0)
	(coding '())
	(sls 0)
	(srs 0)
	(str-len 0)
	(err-typ #f)
	)
    ;;(y-or-n "s=~a,n=~a,ls=~a,rs=~a.cyl-l=~a" s n ls rs cyl-l)
    (tr-string (loc c s n)
	       (set! cyl-loc loc)
	       ;;(y-or-n "loc=~a,c=~a,cyl-l=~a.\n" loc c cyl-l)
	       (cond
		((or (rd-p-deli c)(rd-s-end c))
		 ;;(y-or-n "flag=~a,cyl=~a,ls=~a,rs=~a,st=~a,fn=~a,par=~a.\n" pure-yexp/1class-flag cyl-l ls rs st fn-l par-l)
		 (set! fn-str (merge-str loc fn-l))
		 (set! par-num (length par-l))
		 (cond
		  ((and (= ls 0)(= rs 0))
		   (exit-yexp (list (merge-str loc str-l) (- loc 1) 0 0 (run/err merge-list loc cyl-l) st)))
		  ((= ls rs)
		   (cond
		    (rest-define/flag
		     (set! st '无限参数函数名定义)
		     ;;(set! cyl-l (run/err rewrite-rest-func/yexp '无限参数函数名定义 fn-str (list fn-str '无限参数函数 (append-new-coding fn-str '无限参数函数 par-num))))
		   
		     (set! cyl-l (merge-cyl-fn loc cyl-l st fn-str par-num))
		     ;;(set! cyl-l (run/err rewrite-cyl/rest-define))
		     )
		    (rest/flag
		     (set! cyl-l (run/err rewrite-cyl/rest))
		     (set! st '无限参数函数调用)
		     (set! cyl-l (merge-cyl-fn loc cyl-l st fn-str par-num))
		     ;;(y-or-n "yexp-111:cyl-l=~a\n" cyl-l)
		     )
		    ((and pure-yexp/1class-flag (eq? cyl-define-status 'top-level))
		     
			 (if (eq? st '函数名@表达式)
			     (exit-yexp (list (run/err merge-str loc str-l) (- loc 1) ls rs (run/err merge-cyl-fn loc cyl-l st fn-str par-num) st fn-str (length par-l)))
			     (set! st 'y表达式函数名定义))
			 ;;(y-or-n "TEST-yexp-ls=rs1::cyl-l=~a,st=~a" cyl-l st)
			 (set! fn-str (run/err merge-str loc fn-l))
			 ;;(y-or-n "TEST-yexp-ls=rs2::cyl-l=~a,st=~a,fn-str=~a.\n" cyl-l st fn-str)
			 ;;(set! cyl-l (list (run/err merge-list loc cyl-l) (run/err merge-num loc cyl-l 1) 0 st (cyl-serial) fn-str par-num))
			 (set! cyl-l (merge-cyl-fn loc cyl-l st fn-str par-num))
			 ;;(y-or-n "TEST-yexp-ls=rs3::cyl-l=~a,st=~a" cyl-l st)
			 )
		    (else
		     ;;(y-or-n "TEST-yexp1:cyl-l=~a,st=~a" cyl-l st)
		     (set! st 'y表达式调用)
		     (set! cyl-l (run/err rewrite-cyl/yexp))
		     ;;(y-or-n "TEST-yexp2:cyl-l=~a,st=~a" cyl-l st)
		     (set! cyl-l (run/err merge-cyl-fn loc cyl-l st fn-str par-num))))
		   (exit-yexp (list (run/err merge-str loc str-l) (- loc 1) ls rs cyl-l st fn-str par-num)))
		  (else
		   (exit-yexp (list 'err cyl-loc st '非法语法 (err-format 'yexp loc "遇到分隔符时中括号不匹配。[左中括号=~a][右中括号~a]" ls rs))))))
		((rd-y-beg c)
		 (rewrite-cyl/cons->ciyu)
		 (set! ls (+ ls 1))
		 (let-set! (res-str loc ls rs res-cyl st res-fn-str res-par-num) (run/err yexp s (+ loc 1) ls rs))
		 ;;(y-or-n "test:nth 3 res-cyl=~a,res-cyl=~a" (nth 4 res-cyl) res-cyl)
		 (set! cyl-loc loc)
		 (if (not (eq? (nth 3 res-cyl) '词语参数))
		     (set! pure-yexp/1class-flag #f))
		 ;;(if (or rest/flag rest-define/flag) (set! par-l (run/err rewrite-par-list/rest-par loc par-l fn-str)))
		 (push res-str str-l)
		 (push y-deli fn-l)
		 (push res-str par-l)
		 (if (eq? st 'y表达式函数名定义) (set! res-cyl (run/err rewrite-cyl/yexp-call loc res-cyl res-fn-str res-par-num)))
		 (push res-cyl cyl-l)
		 ;;(y-or-n "yexp:res-cyl-l=~a,flag=~a\n" res-cyl pure-yexp/1class-flag)
		 )
		((rd-y-end c)
		 ;;(y-or-n "y-end:cyl=~a,flag=~a,fn=~a,par=~a\n" cyl-l pure-yexp/1class-flag fn-l par-l)
		 (set! rs (+ rs 1))
		 (if (<= rs ls)
		     (begin
		       (set! fn-str (merge-str loc fn-l))
		       (set! par-num (length par-l))
		     (cond
		      (rest/flag
		       (set! st '无限参数函数调用)
		       (set! cyl-l (run/err rewrite-cyl/rest))
		       (set! cyl-l (merge-cyl-fn loc cyl-l st fn-str par-num))
		       ;;(y-or-n "yexp-222:cyl-l=~a\n" cyl-l)
		       (exit-yexp (list (run/err merge-str loc str-l) loc ls rs cyl-l st fn-str par-num))
		       )
		      ((and (null? fn-l)(null? par-l))
		       (exit-yexp (list "" loc ls rs (list "" 0 sys-nil '系统常量 (cyl-serial)) '常量)))
		      ((null? par-l)
		       (set! cyl-l (rewrite-cyl/ciyu))
		       (exit-yexp (list (run/err merge-str loc str-l) loc ls rs  cyl-l  st)))
		      ((and fn-l par-l)
		       (set! st 'y表达式调用)
		       (set! cyl-l (merge-cyl-fn loc (rewrite-cyl/yexp) st fn-str par-num))
		       (exit-yexp (list (run/err merge-str loc str-l) loc ls rs cyl-l st fn-str par-num)))
		      (else (list 'err cyl-loc st '非法语法 (err-format 'yexp loc "y表达式内部不允许右中括号数多于左中括号。[左中括号=~a][右中括号~a]" ls rs)))))))
		((and (= loc n)(rd-s-guide-sign c))
		 ;;(y-or-n "yexp1:loc=~a,n=~a,guide=~a.\n" loc n cyl-sexp-guide)
		 (if cyl-sexp-guide
		     (push cyl-sexp-guide cyl-sexp-guide-list))
		    
		 (cond
		   ((char=? c #\')
		    (set! st 'lisp-quote)
		    (set! cyl-sexp-guide 'lisp-quote)
		    (set! coding s-exp-quote-coding))
		   ((char=? c #\`)
		    (set! st 'lisp-backquote)
		    (set! cyl-sexp-guide 'lisp-backquote)
		    (set! coding s-exp-backquote-coding))
		   ((char=? c #\,)
		    (set! st 'lisp-unquote)
		    (set! cyl-sexp-guide 'lisp-unquote)
		    (set! coding s-exp-unquote-coding)))
		 ;;(y-or-n "yexp2:guide=~a.\n" cyl-sexp-guide)
		 (set-cyl-list/sexp-guide)
		 (if (null? cyl-sexp-guide-list) (set! cyl-sexp-guide #f) (set! cyl-sexp-guide (pop cyl-sexp-guide-list)))
		 ;;(y-or-n "yexp2:cyl-l=~a.\n" cyl-l)
		 )
		((rd-s-beg c)
		 (if (null? cyl-l)
		     (begin
		       (set-cyl-list/sexp-begin)
		       ;;(y-or-n "yexp:sexp:cyl-l=~a\n" cyl-l)
		       )
		     (set-par-list/aux))
		 )
		((rd-at-mark c)
		 ;;(y-or-n "yexp[at]:loc=~a,cyl-l=~a,cyl-sexp-flag=~a\n" loc cyl-l cyl-sexp-flag)
		 (set-dict-list/at)
		 )
		((rd-and-mark c)
		 (set-dict-list/and)
		 )
		((rd-str-mark c)
		 ;;(y-or-n "yexp000:cyl-l=~a,st=~a,loc=~a.\n" cyl-l st loc)
		 (if (null? st)
		     (begin
		       (let-set! (res-str loc st) (run/err get-str s loc))
		       (set! cyl-loc loc)
		       ;;(y-or-n "yexp-str111:cyl-l=~a,st=~a,loc=~a.\n" cyl-l st loc)
		       (push res-str str-l)
		       (push res-str fn-l)
		       (push (list res-str (string-length res-str) 0 '字符串 (cyl-serial)) cyl-l))
		     (exit-yexp (list 'err cyl-loc st '非法语法 (err-format 'yexp loc "y表达式中不允许存在未经分隔符分割的两个连续语法单位。上一个是[~a],当前一个是字符串。" st)))))
		((rd-pound c)
		 (if (null? st)
		     (begin
		       (let-set! (res-str loc st) (run/err get-cons s loc))
		       (set! cyl-loc loc)
		       (push res-str str-l)
		       (push res-str fn-l)
		       (push (list res-str (string-length res-str) 0 st (cyl-serial)) cyl-l))
		     (exit-yexp (list 'err cyl-loc st '非法语法 (err-format 'yexp loc "y表达式中不允许存在未经分隔符分割的两个连续语法单位。上一个是[~a],当前一个是#常量。" st)))))
		(else (let-set! (res-str loc st coding) (run/err get-sym s loc))
		      ;;(y-or-n "res-str=~a,st=~a\n" res-str st)
		      (set! cyl-loc loc)
		      (if (and (null? fn-l) (or (eq? st '系统宏)(eq? st '系统关键字)))
			  (exit-yexp (list res-str loc ls rs (list res-str (string-length res-str) coding st (cyl-serial)) st)))
		      (set-cyl-list/ciyu-aux)
		      ;;(y-or-n "YEXP-ELSE:loc=~a,cyl-l=~a\n" loc cyl-l)
		      )
		))
    ;;handle string end.
    ;;(y-or-n "TEST:cyl-l=~a,ls=~a,rs=~a." cyl-l ls rs)
    (let ((loc len)
	  )
      (set! cyl-loc loc)
      (set! fn-str (merge-str loc fn-l))
      (set! par-num (length par-l))
    (cond
     ((and (= ls 0)(= rs 0))
      (list (run/err merge-str loc str-l) loc 0 0 (run/err merge-list loc cyl-l) st))
     ((= ls rs)
      (cond
       (rest-define/flag
	(set! st '无限参数函数名定义)
	(set! cyl-l (merge-cyl-fn loc cyl-l st fn-str par-num))
	)
       (rest/flag
	(set! st '无限参数函数调用)
	(set! cyl-l (run/err rewrite-cyl/rest))
	(set! cyl-l (merge-cyl-fn loc cyl-l st fn-str par-num))
	;;(y-or-n "yexp-222:cyl-l=~a\n" cyl-l)
	)
       ((and pure-yexp/1class-flag (eq? cyl-define-status 'top-level))
	(if (eq? st '函数名@表达式)
	    (exit-yexp (list (run/err merge-str loc str-l) (- loc 1) ls rs (run/err merge-cyl-fn loc cyl-l st fn-str par-num) st fn-str (length par-l)))
	    (set! st 'y表达式函数名定义))
	(set! fn-str (run/err merge-str loc fn-l))
	(set! cyl-l (merge-cyl-fn loc cyl-l st fn-str par-num)))
       (else
	(set! st 'y表达式调用)
	(set! fn-str (run/err merge-str loc fn-l))
	(set! cyl-l (merge-cyl-fn loc (run/err rewrite-cyl/yexp) st fn-str par-num))))
      (list (run/err merge-str loc str-l) loc ls rs cyl-l st fn-str par-num))
     (else
      (list 'err cyl-loc st '非法语法 (err-format 'yexp loc "遇到分隔符时中括号不匹配。[左中括号=~a][右中括号~a]" ls rs)))))))
    
	       
	       
		      
		      
		      
		       
		 
		       
			    
		 
		   
		     
		 
		 
		     
		 
		  
			     
			     
			 
    
  
